package Sort;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class FileMergeSort {


    @Before
    public void init() throws Exception {
        File sortFileDir = new File(FileMergeSort.class.getResource("").getFile() + "/sortfile");
        if (!sortFileDir.exists()) {
            sortFileDir.mkdirs();
        }
        File bigFile = new File(sortFileDir.getPath() + "/big-file.txt");
        if (bigFile.exists()) {
            bigFile.delete();
        }
        bigFile.createNewFile();
        String newLine = System.getProperty("line.separator");
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(bigFile));
        new Random(System.currentTimeMillis()).longs(100_000_000).forEach((l) -> {
            try {
                outputStream.write(String.valueOf(Math.abs(l)).getBytes());
                outputStream.write(newLine.getBytes());
                if (l % 1000 == 0) {
                    outputStream.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        outputStream.flush();
        outputStream.close();
    }

    @Test
    public void fileMergeSort() {
        System.out.println(1111);
    }
}
